<template>
  <!-- 头部导航栏 -->
  <div class="hcontent">
    <van-icon name="wap-home-o" size="40px" />
    <p class="text">网易严选</p>
    <van-icon name="search" size="40px" />
    <van-icon name="shopping-cart-o" size="40px" />
  </div>

  <div class="empty3"></div>

  <!-- 网易图片 -->
  <div class="center">
    <van-image
      width="150"
      height="50"
      src="https://yanxuan.nosdn.127.net/static-union/164793255107785e.png"
    />
  </div>

  <div class="container">
    <van-form @submit="onSubmit">
      <!-- 手机号 -->
      <van-cell-group inset>
        <van-field
          v-model="phone"
          name="手机号"
          border
          placeholder="手机号"
          :rules="[
            { required: true, message: '请输入手机号' },
            { pattern, message: '请输入正确的手机号' }
          ]"
        />
      </van-cell-group>

      <!-- 密码 -->
      <van-cell-group inset>
        <van-field
          v-model="password"
          required
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>

      <!-- 验证码 -->
      <van-cell-group inset>
        <van-field center clearable placeholder="请输入短信验证码" v-model="code">
          <template #button>
            <van-button size="small" type="default" @click="getSendCode" block
              >获取验证码</van-button
            >
          </template>
        </van-field>
      </van-cell-group>
    </van-form>
  </div>
  <div class="empty2"></div>

  <!-- 去登录 -->
  <p style="float: right" class="toLogin" @click="GoLogin">去登录</p>

  <!-- 使用密码登录 -->
  <div class="password" style="margin-top: 20px">
    <!-- @click="getNumber" -->
  </div>

  <div class="empty"></div>

  <!-- 登录大按钮 -->
  <div class="iphone">
    <van-button type="danger" block style="width: 350px; height: 52px" @click="getNumber"
      >注册账号
    </van-button>
  </div>

  <div class="Email">其他登录方式 ></div>
</template>

<script lang="ts">
export default {
  name: 'EmLogin'
}
</script>

<script lang="ts" setup>
import { reqRegister, reqSendCode } from '@/api/user'
import router from '@/router'
import { showFailToast, showSuccessToast } from 'vant'
import { ref } from 'vue'

const phone = ref('')
const password = ref('')
const code = ref()

// 手机号验证规则
const pattern =
  /^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/

// 获取验证码
const getSendCode = async () => {
  // console.log(phone.value)
  // console.log(verification.value)

  if (phone.value) {
    const res = await reqSendCode(phone.value)
    if (res.code === 200) {
      code.value = res.data
    }
  }
}

// 注册账号
const getNumber = async () => {
  const res = await reqRegister(phone.value, password.value, code.value)

  if (res.code === 200) {
    showSuccessToast('注册成功！')
    phone.value = ''
    password.value = ''
    code.value = ''
    router.push('/iphone')
  } else {
    showFailToast('注册失败，请重试~')
    phone.value = ''
    password.value = ''
    code.value = ''
    // router.push('/email')
  }
}

// 提交验证
const onSubmit = () => {
  getNumber()
}

// 去登录按钮
const GoLogin = () => {
  router.push('/iphone')
}
</script>

<style lang="less" scoped>
.hcontent {
  background-color: #f2f5f4;
  display: flex;
  justify-content: space-between;
  // height: 50px;
  line-height: 50px;
  text-align: center;
}
.center {
  text-align: center;
  margin-bottom: 35px;
}
.hcontent span {
  line-height: 50px;
  text-align: center;
  font-size: 20px;
}
.S1 {
  display: block;
  float: left;
}
.S2 {
  display: block;
  float: right;
}
.Email {
  margin-top: 80px;
  text-align: center;
  font-size: 15px;
}
.toLogin {
  font-size: 13px;
  margin-right: 8px;
}
.empty {
  height: 30px;
  width: 375px;
  background-color: white;
}
.empty2 {
  height: 10px;
  width: 375px;
  background-color: white;
}
.iphone {
  margin-left: 12px;
}
.center {
  text-align: center;
  margin-bottom: 100px;
  // margin-top: 70px;
}
.empty3 {
  height: 40px;
  width: 375px;
  background-color: white;
}
.text {
  line-height: 40px;
}
</style>
